{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "207cea24",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "14869d3c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    1\n",
       "1    2\n",
       "2    3\n",
       "3    4\n",
       "4    5\n",
       "dtype: int64"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# pandas基础数据类型\n",
    "# series\n",
    "list1 = [1,2,3,4,5]\n",
    "pd.Series(list1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "42c6fe9e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "a    1\n",
       "b    2\n",
       "c    3\n",
       "dtype: int64"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dict1 = {'a':1,'b':2,'c':3}\n",
    "s1 = pd.Series(dict1)\n",
    "s1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "e57adcb5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'a'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s1.index[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "3047c731",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s1.values\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "96352566",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>7</td>\n",
       "      <td>8</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   0  1  2\n",
       "0  1  2  3\n",
       "1  4  5  6\n",
       "2  7  8  9"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# dataframe\n",
    "# dataframe是一个二维的表格数据结构，每列可以是不同的数据类型\n",
    "# 可以通过列表、字典、series等数据结构创建dataframe\n",
    "# 列表创建dataframe\n",
    "list2 = [[1,2,3],[4,5,6],[7,8,9]]\n",
    "df1 = pd.DataFrame(list2)\n",
    "df1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "77271109",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello\\world\n"
     ]
    }
   ],
   "source": [
    "# 字符转义\n",
    "# 当字符串中包含特殊字符时，需要使用转义字符来表示\n",
    "# 例如，\\n表示换行符，\\t表示制表符等\n",
    "# 下面是一个示例\n",
    "s3 = R'hello\\world'\n",
    "print(s3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "ab56f6a8",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "c:\\Users\\1126\\Desktop\\htt\\ck\\.venv\\Lib\\site-packages\\openpyxl\\styles\\stylesheet.py:237: UserWarning: Workbook contains no default style, apply openpyxl's default\n",
      "  warn(\"Workbook contains no default style, apply openpyxl's default\")\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>学号</th>\n",
       "      <th>姓名</th>\n",
       "      <th>专业</th>\n",
       "      <th>班级</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>P241012250</td>\n",
       "      <td>白玛央金</td>\n",
       "      <td>新闻学</td>\n",
       "      <td>2024级新闻学1班</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>P241012251</td>\n",
       "      <td>蔡召宇</td>\n",
       "      <td>新闻学</td>\n",
       "      <td>2024级新闻学1班</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>P241012254</td>\n",
       "      <td>鄂德超</td>\n",
       "      <td>新闻学</td>\n",
       "      <td>2024级新闻学1班</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           学号    姓名   专业          班级\n",
       "0  P241012250  白玛央金  新闻学  2024级新闻学1班\n",
       "1  P241012251   蔡召宇  新闻学  2024级新闻学1班\n",
       "2  P241012254   鄂德超  新闻学  2024级新闻学1班"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 使用pandas读取数据文件\n",
    "# 读取Excel文件\n",
    "df2 = pd.read_excel(R'data\\xls\\24news1.xlsx')\n",
    "df2.head(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "3df5f256",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>day</th>\n",
       "      <th>STOCK_CODE</th>\n",
       "      <th>open</th>\n",
       "      <th>close</th>\n",
       "      <th>maximum</th>\n",
       "      <th>minimum</th>\n",
       "      <th>volume</th>\n",
       "      <th>TURNOVER</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2001/8/27</td>\n",
       "      <td>6005191</td>\n",
       "      <td>34.51</td>\n",
       "      <td>35.55</td>\n",
       "      <td>37.78</td>\n",
       "      <td>32.85</td>\n",
       "      <td>406318</td>\n",
       "      <td>1410347008</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2001/8/28</td>\n",
       "      <td>6005191</td>\n",
       "      <td>34.99</td>\n",
       "      <td>36.86</td>\n",
       "      <td>37.00</td>\n",
       "      <td>34.61</td>\n",
       "      <td>129647</td>\n",
       "      <td>463463008</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2001/8/28</td>\n",
       "      <td>6005191</td>\n",
       "      <td>34.99</td>\n",
       "      <td>36.86</td>\n",
       "      <td>37.00</td>\n",
       "      <td>34.61</td>\n",
       "      <td>129647</td>\n",
       "      <td>463463008</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         day  STOCK_CODE   open  close  maximum  minimum  volume    TURNOVER\n",
       "0  2001/8/27     6005191  34.51  35.55    37.78    32.85  406318  1410347008\n",
       "1  2001/8/28     6005191  34.99  36.86    37.00    34.61  129647   463463008\n",
       "2  2001/8/28     6005191  34.99  36.86    37.00    34.61  129647   463463008"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 使用pandas读取csv格式数据文件\n",
    "# 读取CSV文件\n",
    "df3 = pd.read_csv(R'data\\csv\\600519.csv')\n",
    "df3.head(3)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "6640e4b7",
   "metadata": {},
   "outputs": [
    {
     "ename": "ModuleNotFoundError",
     "evalue": "No module named 'pdfplumber'",
     "output_type": "error",
     "traceback": [
      "\u001b[31m---------------------------------------------------------------------------\u001b[39m",
      "\u001b[31mModuleNotFoundError\u001b[39m                       Traceback (most recent call last)",
      "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[19]\u001b[39m\u001b[32m, line 2\u001b[39m\n\u001b[32m      1\u001b[39m \u001b[38;5;66;03m# 读取pdf格式中的表格\u001b[39;00m\n\u001b[32m----> \u001b[39m\u001b[32m2\u001b[39m \u001b[38;5;28;01mimport\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mpdfplumber\u001b[39;00m\n\u001b[32m      4\u001b[39m pdf =  pdfplumber.open(\u001b[33mR\u001b[39m\u001b[33m'\u001b[39m\u001b[33mdata\u001b[39m\u001b[33m\\\u001b[39m\u001b[33mpdf\u001b[39m\u001b[33m\\\u001b[39m\u001b[33m高考核心词汇1278.pdf\u001b[39m\u001b[33m'\u001b[39m)\n\u001b[32m      6\u001b[39m table = []\n",
      "\u001b[31mModuleNotFoundError\u001b[39m: No module named 'pdfplumber'"
     ]
    }
   ],
   "source": [
    "# 读取pdf格式中的表格\n",
    "import pdfplumber\n",
    "\n",
    "pdf =  pdfplumber.open(R'data\\pdf\\高考核心词汇1278.pdf')\n",
    "\n",
    "table = []\n",
    "# len(pdf.pages)获取全部pdf页数\n",
    "for i in range(len(pdf.pages)):\n",
    "    # 通过循环逐页读取当前页面中的表格\n",
    "    page = pdf.pages[i + 1]\n",
    "    table.append(page.extract_table())\n",
    "# 将表格转换为Pandas中的DataFrame对象\n",
    "table1_df = pd.DataFrame(table[2:])\n",
    "table1_df.head(2)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0fa282f2",
   "metadata": {},
   "outputs": [
    {
     "ename": "ImportError",
     "evalue": "Missing optional dependency 'pyreadstat'.  Use pip or conda to install pyreadstat.",
     "output_type": "error",
     "traceback": [
      "\u001b[31m---------------------------------------------------------------------------\u001b[39m",
      "\u001b[31mModuleNotFoundError\u001b[39m                       Traceback (most recent call last)",
      "\u001b[36mFile \u001b[39m\u001b[32mc:\\Users\\1126\\Desktop\\htt\\ck\\.venv\\Lib\\site-packages\\pandas\\compat\\_optional.py:135\u001b[39m, in \u001b[36mimport_optional_dependency\u001b[39m\u001b[34m(name, extra, errors, min_version)\u001b[39m\n\u001b[32m    134\u001b[39m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[32m--> \u001b[39m\u001b[32m135\u001b[39m     module = \u001b[43mimportlib\u001b[49m\u001b[43m.\u001b[49m\u001b[43mimport_module\u001b[49m\u001b[43m(\u001b[49m\u001b[43mname\u001b[49m\u001b[43m)\u001b[49m\n\u001b[32m    136\u001b[39m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mImportError\u001b[39;00m:\n",
      "\u001b[36mFile \u001b[39m\u001b[32m~\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\importlib\\__init__.py:90\u001b[39m, in \u001b[36mimport_module\u001b[39m\u001b[34m(name, package)\u001b[39m\n\u001b[32m     89\u001b[39m         level += \u001b[32m1\u001b[39m\n\u001b[32m---> \u001b[39m\u001b[32m90\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43m_bootstrap\u001b[49m\u001b[43m.\u001b[49m\u001b[43m_gcd_import\u001b[49m\u001b[43m(\u001b[49m\u001b[43mname\u001b[49m\u001b[43m[\u001b[49m\u001b[43mlevel\u001b[49m\u001b[43m:\u001b[49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mpackage\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mlevel\u001b[49m\u001b[43m)\u001b[49m\n",
      "\u001b[36mFile \u001b[39m\u001b[32m<frozen importlib._bootstrap>:1387\u001b[39m, in \u001b[36m_gcd_import\u001b[39m\u001b[34m(name, package, level)\u001b[39m\n",
      "\u001b[36mFile \u001b[39m\u001b[32m<frozen importlib._bootstrap>:1360\u001b[39m, in \u001b[36m_find_and_load\u001b[39m\u001b[34m(name, import_)\u001b[39m\n",
      "\u001b[36mFile \u001b[39m\u001b[32m<frozen importlib._bootstrap>:1324\u001b[39m, in \u001b[36m_find_and_load_unlocked\u001b[39m\u001b[34m(name, import_)\u001b[39m\n",
      "\u001b[31mModuleNotFoundError\u001b[39m: No module named 'pyreadstat'",
      "\nDuring handling of the above exception, another exception occurred:\n",
      "\u001b[31mImportError\u001b[39m                               Traceback (most recent call last)",
      "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[75]\u001b[39m\u001b[32m, line 2\u001b[39m\n\u001b[32m      1\u001b[39m \u001b[38;5;66;03m# 使用pandas读取spss格式数据文件\u001b[39;00m\n\u001b[32m----> \u001b[39m\u001b[32m2\u001b[39m df4 = \u001b[43mpd\u001b[49m\u001b[43m.\u001b[49m\u001b[43mread_spss\u001b[49m\u001b[43m(\u001b[49m\u001b[33;43mR\u001b[39;49m\u001b[33;43m'\u001b[39;49m\u001b[33;43mdata\u001b[39;49m\u001b[33;43m\\\u001b[39;49m\u001b[33;43msav\u001b[39;49m\u001b[33;43m\\\u001b[39;49m\u001b[33;43m测试成绩.sav\u001b[39;49m\u001b[33;43m'\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[32m      3\u001b[39m df4.head(\u001b[32m3\u001b[39m)\n",
      "\u001b[36mFile \u001b[39m\u001b[32mc:\\Users\\1126\\Desktop\\htt\\ck\\.venv\\Lib\\site-packages\\pandas\\io\\spss.py:58\u001b[39m, in \u001b[36mread_spss\u001b[39m\u001b[34m(path, usecols, convert_categoricals, dtype_backend)\u001b[39m\n\u001b[32m     22\u001b[39m \u001b[38;5;28;01mdef\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34mread_spss\u001b[39m(\n\u001b[32m     23\u001b[39m     path: \u001b[38;5;28mstr\u001b[39m | Path,\n\u001b[32m     24\u001b[39m     usecols: Sequence[\u001b[38;5;28mstr\u001b[39m] | \u001b[38;5;28;01mNone\u001b[39;00m = \u001b[38;5;28;01mNone\u001b[39;00m,\n\u001b[32m     25\u001b[39m     convert_categoricals: \u001b[38;5;28mbool\u001b[39m = \u001b[38;5;28;01mTrue\u001b[39;00m,\n\u001b[32m     26\u001b[39m     dtype_backend: DtypeBackend | lib.NoDefault = lib.no_default,\n\u001b[32m     27\u001b[39m ) -> DataFrame:\n\u001b[32m     28\u001b[39m \u001b[38;5;250m    \u001b[39m\u001b[33;03m\"\"\"\u001b[39;00m\n\u001b[32m     29\u001b[39m \u001b[33;03m    Load an SPSS file from the file path, returning a DataFrame.\u001b[39;00m\n\u001b[32m     30\u001b[39m \n\u001b[32m   (...)\u001b[39m\u001b[32m     56\u001b[39m \u001b[33;03m    >>> df = pd.read_spss(\"spss_data.sav\")  # doctest: +SKIP\u001b[39;00m\n\u001b[32m     57\u001b[39m \u001b[33;03m    \"\"\"\u001b[39;00m\n\u001b[32m---> \u001b[39m\u001b[32m58\u001b[39m     pyreadstat = \u001b[43mimport_optional_dependency\u001b[49m\u001b[43m(\u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mpyreadstat\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[32m     59\u001b[39m     check_dtype_backend(dtype_backend)\n\u001b[32m     61\u001b[39m     \u001b[38;5;28;01mif\u001b[39;00m usecols \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n",
      "\u001b[36mFile \u001b[39m\u001b[32mc:\\Users\\1126\\Desktop\\htt\\ck\\.venv\\Lib\\site-packages\\pandas\\compat\\_optional.py:138\u001b[39m, in \u001b[36mimport_optional_dependency\u001b[39m\u001b[34m(name, extra, errors, min_version)\u001b[39m\n\u001b[32m    136\u001b[39m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mImportError\u001b[39;00m:\n\u001b[32m    137\u001b[39m     \u001b[38;5;28;01mif\u001b[39;00m errors == \u001b[33m\"\u001b[39m\u001b[33mraise\u001b[39m\u001b[33m\"\u001b[39m:\n\u001b[32m--> \u001b[39m\u001b[32m138\u001b[39m         \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mImportError\u001b[39;00m(msg)\n\u001b[32m    139\u001b[39m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[32m    141\u001b[39m \u001b[38;5;66;03m# Handle submodules: if we have submodule, grab parent module from sys.modules\u001b[39;00m\n",
      "\u001b[31mImportError\u001b[39m: Missing optional dependency 'pyreadstat'.  Use pip or conda to install pyreadstat."
     ]
    }
   ],
   "source": [
    "# 使用pandas读取spss格式数据文件\n",
    "df4 = pd.read_spss(R'data\\sav\\测试成绩.sav')\n",
    "df4.head(3)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
